import { Redirect } from "react-router-dom"
import { lazy } from 'react'
import Layout from "@/pages/layout/Layout/Layout"
import Index from "../pages/layout/index/Index"
var _import = (page) => lazy(() => import(`@/pages/${page}`))
const router = {
  basename: "/",
  routes: [
    {
      path: "/index",
      component: Layout,
      // exact: true,
      children: [
        {
          path: "/index",
          component: Index,
          exact: true,
          meta: {
            title: "首页"
          }
        },
      ],
    },
    {
      path: "/login",
      component: _import("login/Login"),
      meta: {
        title: "登录",
      },
    },
    {
      path: "/",
      exact: true,
      render: () => <Redirect to="/index"></Redirect>
    },
    { path: "/404", exact: true, render: () => <div>404</div> },
  ],

}

router.beforeEach = (to, form, history) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  let token = localStorage.getItem("token")
  if (to.meta.requiresAuth && !token) {
    history.replace("/login")
  }
}
export default router